/*
 * @Author: 0x9DEFA478
 * @Date: 2021-09-21 16:48:16
 * @LastEditTime: 2021-09-22 12:11:05
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "GT1151.h"




extern const unsigned char GT1151_ConfigTable[];






static int Init(GT1151* gt1151){
  unsigned char* AddrArray;//2+vGT1151_ConfigTable_Length+3 byte
  unsigned char* Config_Version;//vGT1151_ConfigTable_Length byte
	unsigned short checksum;
  int i;
  int r;


  AddrArray=gt1151->TxBuffer;
  Config_Version=gt1151->RxBuffer;

  gt1151->LL_Function.Reset();

  AddrArray[0]=0x81U;
  AddrArray[1]=0x40U;
  gt1151->LL_Function.I2C_Read(gt1151->Addr,AddrArray,2,gt1151->RxBuffer,4);

  gt1151->ProductID[0]=gt1151->RxBuffer[0];
  gt1151->ProductID[1]=gt1151->RxBuffer[1];
  gt1151->ProductID[2]=gt1151->RxBuffer[2];
  gt1151->ProductID[3]=gt1151->RxBuffer[3];


  AddrArray[0]=0x80U;
  AddrArray[1]=0x50U;
  r=gt1151->LL_Function.I2C_Read(gt1151->Addr,AddrArray,2,Config_Version,vGT1151_ConfigTable_Length);
  if(r!=0){
    return r;
  }

  if(Config_Version[0]<GT1151_ConfigTable[0]){
    checksum=0;
    for(i=0;i<vGT1151_ConfigTable_Length;i+=2){
      AddrArray[2+i]=GT1151_ConfigTable[i];
      AddrArray[2+i+1]=GT1151_ConfigTable[i+1];
      checksum += ((GT1151_ConfigTable[i]<<8)|GT1151_ConfigTable[i+1]);
    }
    checksum =(~checksum)+1;
    AddrArray[2+vGT1151_ConfigTable_Length]=  (unsigned char)(checksum>>8);
    AddrArray[2+vGT1151_ConfigTable_Length+1]=(unsigned char)checksum;
    AddrArray[2+vGT1151_ConfigTable_Length+2]=0x01;
    gt1151->LL_Function.I2C_Write(gt1151->Addr,AddrArray,2+vGT1151_ConfigTable_Length+3);
    if(r!=0){
      return r;
    }
  }

  return 0;
}

/**
 * @brief GT1151 初始化
 * @param gt1151 GT1151用于容纳句柄
 * @param Addr 器件地址
 * @param TxBuffer 发送缓存
 * @param RxBuffer 接收缓存
 * @param LL_Function 底层方法
 * @return 0:成功 其他:失败
 */
int GT1151_Init(GT1151* gt1151,unsigned char Addr,unsigned char* TxBuffer,unsigned char* RxBuffer,GT1151_LL_Function* LL_Function){


  gt1151->Addr=Addr;

  gt1151->TxBuffer=TxBuffer;
  gt1151->RxBuffer=RxBuffer;

  gt1151->LL_Function=LL_Function[0];

  return Init(gt1151);
}

/**
 * @brief 刷新点位信息 不要太频繁调用此方法(触摸芯片可能会反应不过来)
 * @param gt1151 GT1151句柄
 * @return 0:刷新成功 其他:刷新失败
 */
int GT1151_RefreshTouchPoint(GT1151* gt1151){
  unsigned char* AddrArray;//2byte
  unsigned char* PointData;//80byte
  unsigned char* Point;
  unsigned char _814E;
  int PointNum;
  int track_id;
  int loc_x;
  int loc_y;
  int i;
  int r;


  AddrArray=gt1151->TxBuffer;
  PointData=gt1151->RxBuffer;

  AddrArray[0]=0x81U;
  AddrArray[1]=0x4EU;
  r=gt1151->LL_Function.I2C_Read(gt1151->Addr,AddrArray,2,PointData,1);
  if(r!=0){
    return r;
  }
  _814E=PointData[0];
  PointNum=_814E&0x0FU;

  if((_814E&0x80U)!=0){
		AddrArray[0]=0x81U;
		AddrArray[1]=0x4EU;
		AddrArray[2]=0x00;
		r=gt1151->LL_Function.I2C_Write(gt1151->Addr,AddrArray,3);
    if(r!=0){
      return r;
    }
  }

  for(i=0;i<vGT1151_NumOfPoint;i++){
    gt1151->PointData[i].S=0;
  }

  if((PointNum!=0)&&(PointNum<(vGT1151_NumOfPoint+1))){

    AddrArray[0]=0x81U;
    AddrArray[1]=0x4FU;
    r=gt1151->LL_Function.I2C_Read(gt1151->Addr,AddrArray,2,PointData,8*PointNum);
    if(r!=0){
      return r;
    }

    Point=PointData;
    for(i=0;i<PointNum;i++){
      if((Point[0]&0x80U)==0){
        track_id=Point[0]&0x0FU;

        loc_x=((unsigned short)Point[1]&0xFFU)+(((unsigned short)Point[2]<<8)&0xFF00U);
        loc_y=((unsigned short)Point[3]&0xFFU)+(((unsigned short)Point[4]<<8)&0xFF00U);

        if((loc_x>=0)&&(loc_y>=0)&&(loc_x<vGT1151_PanelWidth)&&(loc_y<vGT1151_PanelHeight)){
          gt1151->PointData[track_id].S=-1;

          gt1151->PointData[track_id].X=loc_x;
          gt1151->PointData[track_id].Y=loc_y;

          gt1151->PointData[track_id].W=Point[5];
          gt1151->PointData[track_id].H=Point[6];
        }
        
      }

      Point=&Point[8];
    }

  }

  return 0;
}











